package io.github.talelin.latticy.controller.v1;

import cn.hutool.core.date.DateUtil;
import io.github.talelin.autoconfigure.exception.ParameterException;
import io.github.talelin.latticy.utils.ExcelUtils;
import io.github.talelin.latticy.utils.FtlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.service.AcceptService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Positive;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.github.talelin.latticy.dto.accept.CreateOrUpdateAcceptDTO;
import io.github.talelin.latticy.model.AcceptDO;

/**
 * @author vvboy
 */
@RestController
@RequestMapping("/v1/accept")
@Validated
@Slf4j
public class AcceptController {

    @Autowired
    private AcceptService acceptService;

    @GetMapping("/{id}")
    public AcceptDO getAccept(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        AcceptDO accept = acceptService.getById(id);
        if (accept == null) {
            throw new NotFoundException("找不到", 10022);
        }
        return accept;
    }

    @GetMapping("")
    public List<AcceptDO> getAccepts() {
        return acceptService.findAll();
    }


    @PostMapping("")
    public CreatedVO createAccept(@RequestBody @Validated CreateOrUpdateAcceptDTO validator) {
        acceptService.createAccept(validator);
        return new CreatedVO("新建成功");
    }


    @PutMapping("/{id}")
    public UpdatedVO updateAccept(@PathVariable("id") @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated CreateOrUpdateAcceptDTO validator) {
        AcceptDO accept = acceptService.getById(id);
        if (accept == null) {
            throw new NotFoundException(10022);
        }
        acceptService.updateAccept(accept, validator);
        return new UpdatedVO("修改成功");
    }


    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除", module = "")
    public DeletedVO deleteAccept(@PathVariable("id") @Positive(message = "{id.positive}") Integer id) {
        AcceptDO accept = acceptService.getById(id);
        if (accept == null) {
            throw new ParameterException(10022);
        }
        acceptService.deleteById(accept.getId());
        return new DeletedVO("删除成功");
    }

    @RequestMapping("exportAllWord")
    @CrossOrigin
    public void exportAllWord(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") Integer id) throws IOException {
        request.getSession().setAttribute("token", "Y");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            AcceptDO accept = acceptService.getById(id);
            map.put("data", accept);
            FtlUtils.reportPeisOrgReservation(map,response,"accept.ftl");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    @RequestMapping("exportAllExcel")
    @CrossOrigin
    public void exportAllExcel(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") Integer id) throws IOException {
        request.getSession().setAttribute("token", "Y");
        Map<String, Object> map = new HashMap<String, Object>();
        String templateFileName = ClassUtils.getDefaultClassLoader().getResource("").getPath()
                + "static/file/accept.xls";
        String destFileName = "维修受理单.xls";
        try {
            AcceptDO accept = acceptService.getById(id);
            map.put("data", accept);
            ExcelUtils.outputToExcel(map, templateFileName, destFileName, response, request);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    @RequestMapping("exportAllPdf")
    @CrossOrigin
    public void exportAllPdf(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") Integer id) throws IOException {
        request.getSession().setAttribute("token", "Y");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            AcceptDO accept = acceptService.getById(id);
            map.put("data", accept);
            FtlUtils.reportPeisOrgReservationPdf(map,response,"accept.ftl");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    @GetMapping("statCurMonth")
    public List<String> statCurMonth(){
        return acceptService.statCurMonth();
    }

    @GetMapping("statCurYear")
    public List<String> statCurYear(){
        return acceptService.statCurYear();
    }

    // 受理，未受理
    @GetMapping("statCurRatio")
    public List<Integer> statCurRatio(){
        return acceptService.statCurRatio();
    }
}